package com.example.superoa.common;

import java.io.IOException;
import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.superoa.model.pojos.KqBanci;
import com.example.superoa.model.service.DakaService;

public class Shijian {
	//获取网络百度时间
	public  long getNetworkTime() {
        try {
            URL url=new URL("http://www.baidu.com");
            URLConnection conn=url.openConnection();
            conn.connect();
            long dateL=conn.getDate();
            return dateL;
        }catch (MalformedURLException e) {
            e.printStackTrace();
        }catch (IOException e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return 0 ;
	}
	//JSONArray时间排序
	public JSONArray paixuJSONArray(JSONArray json) {
		//转list 1
		List<JSONObject> list = JSONArray.parseArray(json.toJSONString(), JSONObject.class);
		//转list 2
		//List<JSONObject> list = new ArrayList<JSONObject>();
		//for (int i = 0; i < a.size(); i++) {
//		    list.add((JSONObject) a.get(i));
		//}
		Collections.sort(list, new Comparator<JSONObject>() {
		    @Override
		    public int compare(JSONObject o1, JSONObject o2) {
		    	 SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
		    	 Date one=o1.getDate("sdate");
		    	 Date two=o2.getDate("sdate");
		        Long a = Long.parseLong(sdf.format(one));
		        Long b = Long.parseLong(sdf.format(two));
		        if (a > b) {
		            return -1;
		        } else if(a == b) {
		            return 0;
		        } else
		            return 1;
		        }
		});
		JSONArray jsonArray = JSONArray.parseArray(list.toString());
		return jsonArray;
	}
	//日期是否为该时间段 1为不带年份，2为带年份
	public boolean belongCalendar(Date nowdate,Date begindate,Date enddate,Integer type) {
		Calendar date = Calendar.getInstance();
			date.setTime(nowdate);
		Calendar begin = Calendar.getInstance();
			begin.setTime(begindate);
		Calendar end = Calendar.getInstance();
			end.setTime(enddate);
			if(type==1) {
				end.set(Calendar.YEAR, date.get(Calendar.YEAR));
				begin.set(Calendar.YEAR, date.get(Calendar.YEAR));
			}
		if (date.after(begin) && date.before(end)) {
			return true;
		} else if (nowdate.compareTo(begindate) == 0 || nowdate.compareTo(enddate) == 0) {
			return true;
		} else {
			return false;
		}
	}
	//判断输入日期是否为本月
	public boolean booleanMonth(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String param = sdf.format(date);//参数时间
        String now = sdf.format(new Date(getNetworkTime()));//当前时间
        if (param.equals(now)) {
            return true;
        }
        return false;
	}
	//签到时间比较
	public Integer getQiandao(Time time,Time d_time) {
		String str1="";
		String str2="";
		for (String s : time.toString().split(":")) {
			str1+=s;
		};
		for (String s : d_time.toString().split(":")) {
			str2+=s;
		}
		Integer i=Integer.parseInt(str1);
		Integer ii=Integer.parseInt(str2);
		if(i>ii) {
			return 1;
		}else if(i==ii) {
			return 0;
		}else {
			return -1;
		}
		
	}
	//判断输入日期是否为周末
	public boolean panduanSun(Date date) {
		Calendar cal=Calendar.getInstance();
		cal.setTime(date);
		if(cal.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY){
			return true;
		}
		if(cal.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY){
			return true;
		}
		return false;
	}
	//提出日期中的年月日
	public List<Integer> getyearmonth(Date date){
		List<Integer> list=new ArrayList();
		Calendar cal1=Calendar.getInstance();
		cal1.setTime(date);
		list.add(cal1.get(Calendar.YEAR));
		list.add(cal1.get(Calendar.MONTH)+1);
		list.add(cal1.get(Calendar.DAY_OF_MONTH));
		return list;
	}
	//判断输入日期是否为同一月份
	public Boolean getyuefen(Date date1,Date date2) {
		List<Integer> list1=getyearmonth(date1);
		List<Integer> list2=getyearmonth(date2);
		if(list1.get(0)==list2.get(0)) {
			if(list1.get(1)==list2.get(1)) {
				return true;
			}
		}
		return false;
	}
	//计算相差小时
	public BigDecimal getyuansuan(Time date1,Time date2) {
		long nd = 1000 * 24 * 60 * 60;
	    long nh = 1000 * 60 * 60;
	    // 获得两个时间的毫秒时间差异
	    long diff = date1.getTime() - date2.getTime();
	    // 计算差多少小时
	    long hour = diff % nd / nh;
		return new BigDecimal(hour);
	}
	//算出出勤时间
	public BigDecimal getShichu(Time dao,Time tui,KqBanci banci) {
		Time qiandao;
		Time qiantui;
		if(getPanduanShi1(dao,banci.getBDao())==1) {
			qiandao=dao;
		}else {
			qiandao=banci.getBDao();
		}
		if(getPanduanShi1(tui,banci.getBTui())==1) {
			qiantui=banci.getBTui();
		}else {
			qiantui=tui;
		}
		System.out.println(qiandao+"==="+qiantui);
		return getyuansuan(qiantui,qiandao);
	}
	//判断两个时间的大小
	public Integer getPanduanShi1(Date date1,Date date2) {
		SimpleDateFormat sdf=new SimpleDateFormat("HHmmss");
		String str1=sdf.format(date1);
		String str2=sdf.format(date2);
		Long i=Long.parseLong(str1);
		Long ii=Long.parseLong(str2);
		Long bijiao=i-ii;
		if(bijiao>0) {
			return 1;
		}else if(bijiao==0) {
			return 0;
		}else {
			return -1;
		}
	}
	//判断两个时间的大小
	public Integer getPanduanShi2(Date date1,Date date2) {
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
		String str1=sdf.format(date1);
		String str2=sdf.format(date2);
		Long i=Long.parseLong(str1);
		Long ii=Long.parseLong(str2);
		Long bijiao=i-ii;
		if(bijiao>0) {
			return 1;
		}else if(bijiao==0) {
			return 0;
		}else {
			return -1;
		}
	}
	//算出请假时间
	public BigDecimal getQingjiaShi(Date kai,Date jie,Time dao,Time tui) {
		BigDecimal one=getyuansuan(new Time(kai.getTime()),dao);
		BigDecimal two=getyuansuan(new Time(jie.getTime()),tui);
		BigDecimal three=getyuansuan(new Time(jie.getTime()),new Time(kai.getTime()));
		BigDecimal zong=new BigDecimal(0);
		int i=getDaiXing(kai,jie);
		Time k=null;
		Time j=null;
		if(one.compareTo(new BigDecimal(0))==1) {
			k=new Time(kai.getTime());
		}else{
			k=dao;
		}
		if(two.compareTo(new BigDecimal(0))==-1) {
			j=new Time(jie.getTime());
		}else {
			j=tui;
		}
		if(three.compareTo(new BigDecimal(0))==1) {
			zong=zong.add(getyuansuan(j,k));
		}else if(three.compareTo(new BigDecimal(0))==-1){
			zong=zong.add(getyuansuan(tui,k));
			zong=zong.add(getyuansuan(j,dao));
		}
		zong=zong.add(new BigDecimal(i*8));			
		return zong;
	}
	//返回输入的时间的后day天
	public Date getDaiDate(Date kai,int day) {
		Calendar cal1=Calendar.getInstance();
		cal1.setTime(kai);
		int d = cal1.get(Calendar.DATE);  
		cal1.set(Calendar.DATE, d + day);
		return cal1.getTime();
	}
	//返回输入的时间的后hour小时
	public Date getDaiTime(Date kai,int hour) {
		Calendar cal1=Calendar.getInstance();
		cal1.setTime(kai);
		int d = cal1.get(Calendar.HOUR);  
		cal1.set(Calendar.HOUR, d + hour);
		return cal1.getTime();
	}
	//返回输入的时间的前hour小时
	public Date getDaiTime2(Date kai,int hour) {
		Calendar cal1=Calendar.getInstance();
		cal1.setTime(kai);
		int d = cal1.get(Calendar.HOUR);  
		cal1.set(Calendar.HOUR, d - hour);
		return cal1.getTime();
	}
	
	//返回相差天数
	public Integer getDaiXing(Date kai,Date jie) {
		long cha=jie.getTime()-kai.getTime();
		int i=(int)(new Date(cha).getTime()-new Date(0).getTime())/(3600*24*1000);
		return i+1;
	}  
	//算出本月天数
	public Integer getMonthday(int year, int month) {
			int days = 0;
			if (month != 2) {
				switch (month) {
				case 1:
				case 3:
				case 5:
				case 7:
				case 8:
				case 10:
				case 12:
					days = 31;
					break;
				case 4:
				case 6:
				case 9:
				case 11:
					days = 30;
				}
			} else {
				// 闰年
				if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
					days = 29;
				else
					days = 28;
			}
			return days;
	}
	//算出应出勤天数
	public Integer getYingchu(int yue,JSONArray jia,int year,int month) {
		for (String str : getDayByMonth(year,month,0,1)) {
			Date day = null;
			boolean opin=false;
			try {
				day=new SimpleDateFormat("yyyy-MM-dd").parse(str);
			} catch (Exception e) {
				// TODO: handle exception
			}
			for (Object object : jia) {
				JSONObject o=(JSONObject)object;
				int type=1;
				if(o.getInteger("RE_CLASS")==1) {
					type=2;
				}
				if(belongCalendar(day, o.getDate("RE_KAI"), o.getDate("RE_JIE"), type)) {
					opin=true;
					break;
				}
			}
			if(panduanSun(day)||opin) {
				yue=yue-1;
			}
		}
		return yue;
	}
	
	//返回输入时间属于本月的天数
	public Integer getShuDay(List<Integer> kai,List<Integer> jie,int month,int ying) {
			if(kai.get(1)!=month) {
				return jie.get(2);
			}else if(jie.get(1)!=month){
				return kai.get(2)+1;
			}else {
				return jie.get(2)-kai.get(2)+1;
			}
	}
	//存储输入的两个时间之间的日期  1，带年份2，不带年份
	public List<Date> getDays(Integer type,Date kai, Date jie) {
        // 返回的日期集合
        List<Date> days = new ArrayList<Date>();
        	Calendar tempStart = Calendar.getInstance();
            tempStart.setTime(kai);
            Calendar tempEnd = Calendar.getInstance();
            tempEnd.setTime(jie);
            if(type==2) {
            	tempEnd.set(Calendar.YEAR, tempStart.get(Calendar.YEAR));
			}
            tempEnd.add(Calendar.DATE, +1);// 日期加1(包含结束)
            while (tempStart.before(tempEnd)) {
                days.add(tempStart.getTime());
                tempStart.add(Calendar.DAY_OF_YEAR, 1);
            }
            return days;
    }
	//存储输入月份的所有日期,1:月份所有日期，2：输入月份已过所有日期
	public  List<String> getDayByMonth(int one,int tow,int three,int type){
		List<String> list=new ArrayList<>();
		Calendar aCalendar = Calendar.getInstance(Locale.CHINA);
        aCalendar.set(one,tow,three);
        int year = aCalendar.get(Calendar.YEAR);//年份
        int month = aCalendar.get(Calendar.MONTH)+1;//月份
        int day;
        if(type==1) {
        	day= aCalendar.getActualMaximum(Calendar.DATE);
        }else {
        	day=three;
        }
        for (int i = 1; i <= day; i++) {
            String aDate=null;
            if(month<10&&i<10){
                aDate = String.valueOf(year)+"-0"+month+"-0"+i;
            }
            if(month<10&&i>=10){
                aDate = String.valueOf(year)+"-0"+month+"-"+i;
            }
            if(month>=10&&i<10){
                aDate = String.valueOf(year)+"-"+month+"-0"+i;
            }
            if(month>=10&&i>=10){
                aDate = String.valueOf(year)+"-"+month+"-"+i;
            }
            list.add(aDate);
        }
        return list;
	}
	public boolean getPanduan(Date date,Date kai,Date jie) {
		SimpleDateFormat sdf=new SimpleDateFormat("MMdd");
		Integer one=Integer.parseInt(sdf.format(date));
		Integer tow=Integer.parseInt(sdf.format(kai));
		Integer three=Integer.parseInt(sdf.format(jie));
		if(one>=tow&&one<=three) {
			return true;
		}else {
			return false;
		}
	}
}
